﻿namespace AcademyDocs
{
    using AcademyDocs.Web;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using System.Linq;
    using System.Windows;
    using System.ServiceModel.DomainServices.Client;
    using System.Collections.Generic;

    /// <summary>
    /// <see cref="Page"/> class to present information about the current application.
    /// </summary>
    public partial class About : Page
    {
        protected Folder NewFolder { get; set; }
        AcademyDocsDomainContext context = new AcademyDocsDomainContext();
        Folder selectedFolder { get; set; }


        public About()
        {
            InitializeComponent();
            NewFolder = new Folder();
            TextBoxFolderName.DataContext = this;
            string name = WebContext.Current.User.Name;
            

            context.Load(context.GetUserRootFoldersQuery(name)).Completed += (s, a) =>
            {
                LoadOperation<Folder> sender = (LoadOperation<Folder>)s;
                context.Load<Folder>(context.GetUserRootFolderIdQuery(name)).Completed += (sen, arg) =>
                {
                    LoadOperation<Folder> send = (LoadOperation<Folder>)sen;
                    selectedFolder = send.Entities.First();
                    System.Console.WriteLine();
                };
                lbFolders.ItemsSource = sender.Entities;
            };
            this.Title = ApplicationStrings.AboutPageTitle;
        }

        private void ButtonCreateFolder_Click(object sender, RoutedEventArgs e)
        {
            Folder folderToAdd = new Folder { Name = this.TextBoxFolderName.Text, ParentFolderId=selectedFolder.FolderId };
            context.Folders.Add(folderToAdd);
            context.SubmitChanges();
        }

        private void lbFolders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lbFolders.SelectedItem != null)
            {
                selectedFolder = (Folder)this.lbFolders.SelectedItem;
                context.Load(context.GetFoldersByParentFolderIdQuery(selectedFolder.FolderId)).Completed += (s, a) =>
                 {
                     LoadOperation<Folder> receivedFolders = (LoadOperation<Folder>)s;
                     lbFolders.ItemsSource = receivedFolders.Entities;
                 };
            }
        }

        private void buttonUp_Click(object sender, RoutedEventArgs e)
        {
            if (selectedFolder.ParentFolderId!=null)
            {
                
            
            context.Load(context.GetFoldersByParentFolderIdQuery((int)selectedFolder.ParentFolderId)).Completed += (s, a) =>
            {
                LoadOperation<Folder> receivedFolders = (LoadOperation<Folder>)s;
                lbFolders.ItemsSource = receivedFolders.Entities;
                
            };
            LoadParentFolderByID(selectedFolder.ParentFolderId);
            }
        }

        private void LoadParentFolderByID(int? id)
        {
            context.Load<Folder>(context.GetParentFolderIdQuery(id)).Completed +=
                (s, a) =>
                {
                    LoadOperation<Folder> send = (LoadOperation<Folder>)s;
                    selectedFolder = send.Entities.First();
                };
        }
    }
}